package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServicelmpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Override
    public List<AddressBook> list() {
        //获取当前用户 Id
        Long userId = BaseContext.getCurrentId();
        return addressBookMapper.list(userId);
    }

    @Override
    public void add(AddressBook addressBook) {
        //设置用户Id
        addressBook.setUserId(BaseContext.getCurrentId());
        //设置是否为默认地址
        addressBook.setIsDefault(0);
        addressBookMapper.add(addressBook);
    }

    @Override
    public AddressBook getDefault() {
        //获取当前用户Id
        Long userId = BaseContext.getCurrentId();
        return addressBookMapper.getDefault(userId);
    }

    @Override
    public AddressBook getById(Long id) {
        return addressBookMapper.getById(id);
    }

    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    @Override
    public void delete(Long id) {
        addressBookMapper.delete(id);
    }

    @Override
    public void setDefault(AddressBook addressBook) {
        //设置所有默认地址为0
        addressBookMapper.setDefault(BaseContext.getCurrentId());
        //设置当前地址为默认地址
        addressBookMapper.updateDefault(addressBook.getId());
    }
}
